<template>
  <div>
    <div class="ke__search">
      <k-form inline>
        <k-form-item>
          <el-input placeholder="输入用户名、昵称搜索" size="small" v-model="search.key" style="width: 400px"></el-input>
        </k-form-item>

        <k-form-item label="性别">
          <el-select v-model="search.sex" size="small" style="width: 80px">
            <el-option :value="-1" label="全部"></el-option>
            <el-option :value="0" label="女"></el-option>
            <el-option :value="1" label="男"></el-option>
          </el-select>
        </k-form-item>

        <k-form-item>
          <el-button type="info" size="small" native-type="submit" icon="el-icon-search">搜索</el-button>
        </k-form-item>
      </k-form>
    </div>

    <el-table :data="list" v-loading="loading">
      <el-table-column prop="id" label="UID" width="80"></el-table-column>

      <el-table-column prop="avatar" label="头像" width="120">
        <template slot-scope="{ row }">
          <k-image :src="row.avatar" :key="row.avatar" style="width: 64px; height: 64px" />
        </template>
      </el-table-column>

      <el-table-column prop="username" label="用户名" width="200"></el-table-column>

      <el-table-column prop="nickname" label="昵称"></el-table-column>

      <el-table-column label="性别" width="120">
        <template slot-scope="{ row }">
          {{ ['女', '男'][row.sex] }}
        </template>
      </el-table-column>

      <el-table-column prop="score" label="积分" width="150"></el-table-column>

      <el-table-column prop="money" label="余额" width="150"></el-table-column>

      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="{ row, $index }">
          <el-button type="text" icon="el-icon-help" title="充值" @click="onRecharge(row, $index)"></el-button>

          <el-button type="text" icon="el-icon-edit" title="编辑"></el-button>

          <el-button type="text" icon="el-icon-delete" title="删除" @click="onDelete(row, $index)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination :current-page="page" :total="total" @current-change="onPageChange"></el-pagination>

    <!-- 充值窗口 -->
    <k-dialog-recharge v-model="showRecharge" @submit="onRechargeSubmit"></k-dialog-recharge>
  </div>
</template>

<script>
import KDialogRecharge from '../../components/dialog-recharge/index'
export default {
  name: 'Users',
  components: {KDialogRecharge},
  data () {
    return {
      list: [],
      total: 0,
      loading: true,
      page: 1,
      showRecharge: false, // 显示充值窗口
      search: {
        sex: -1
      }
    }
  },
  mounted () {
    this.onLoad()
  },
  methods: {
    onPageChange (page) {
      this.list = []
      this.page = page
      this.onLoad()
    },
    // 加载列表
    onLoad () {
      this.loading = true
      this.$http.get('users', { page: this.page })
        .finally(() => this.loading = false)
        .then(result => {
          this.list = result.list
          this.total = result.total
        })
    },
    // 充值
    onRecharge (row, idx) {
      this.showRecharge = { ...row, index: idx }
    },
    // 提交充值
    onRechargeSubmit (form, done) {
      setTimeout(() => {
        done()
      }, 1000)
    },
    // 删除
    onDelete (row, idx) {
      this.$confirm('确定要删除吗?')
        .then(() => {
          this.list.splice(idx, 1)
        })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
